package cz.muni.fi.pa165.musiclibrary.musiclibraryws;

import java.util.List;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;

import cz.muni.fi.pa165.musiclibrary.api.service.IArtistService;
import cz.muni.fi.pa165.musiclibrary.api.service.IGenreService;
import cz.muni.fi.pa165.musiclibrary.api.service.auth.IAuthService;
import cz.muni.fi.pa165.musiclibrary.api.service.dto.IArtistDTO;
import cz.muni.fi.pa165.musiclibrary.api.service.dto.IGenreDTO;
import cz.muni.fi.pa165.musiclibrary.musiclibraryws.converter.WSConverter;

/**
 *
 * @author Maros Kucbel
 * @date 2012-12-06T16:20:35+0100
 */
@WebService(endpointInterface="cz.muni.fi.pa165.musiclibrary.musiclibraryws.MusicLibraryWS", serviceName="MusicLibraryWS")
public final class MusicLibraryWSImpl implements MusicLibraryWS {

    @Autowired
    IArtistService artistService;
    @Autowired
    IGenreService genreService;
    @Autowired
    IAuthService authService;
    
    @Override
    public WSGenre getGenre(Long idGenre) throws MusicLibraryException {
        IGenreDTO genreDto = this.genreService.getById(idGenre);
        WSGenre genre = genreDto != null ? WSConverter.convertToWSGenre(genreDto) : null;
        return genre;
    }

    @Override
    public List<WSGenre> getGenres() throws MusicLibraryException {
        List<IGenreDTO> genreDtos = this.genreService.getAll();
        return WSConverter.convertToWSGenres(genreDtos);
    }

    @Override
    public void updateGenre(WSGenre genre) throws MusicLibraryException {
        if (genre == null) {
            throw new MusicLibraryException("Genre can not be null.", "Genre can not be null.");
        }
        IGenreDTO genreDto = WSConverter.convertFromWSGenre(genre);
        try {
            this.genreService.update(genreDto, "rest");
        } catch (Throwable ex) {
            throw new MusicLibraryException(ex.getMessage(), ex.getMessage());
        }
    }

    @Override
    public void deleteGenre(WSGenre genre) throws MusicLibraryException {
        if (genre == null) {
            throw new MusicLibraryException("Genre can not be null.", "Genre can not be null.");
        }
        IGenreDTO genreDto = WSConverter.convertFromWSGenre(genre);
        try {
            this.genreService.delete(genreDto, "rest");
        } catch (Throwable ex) {
            throw new MusicLibraryException(ex.getMessage(), ex.getMessage());
        }
    }

    @Override
    public void createGenre(WSGenre genre) throws MusicLibraryException {
        if (genre == null) {
            throw new MusicLibraryException("Genre can not be null.", "Genre can not be null.");
        }
        
        IGenreDTO genreDto = WSConverter.convertFromWSGenre(genre);
        try {
            this.genreService.create(genreDto, "rest");
        } catch (Throwable ex) {
            throw new MusicLibraryException(ex.getMessage(), ex.getMessage());
        }
    }

    @Override
    public WSArtist getArtist(Long idArtist) throws MusicLibraryException {
        IArtistDTO artistDto = this.artistService.getById(idArtist);
        WSArtist artist = artistDto != null ? WSConverter.convertToWSArtist(artistDto) : null;
        return artist;
    }

    @Override
    public List<WSArtist> getArtists() throws MusicLibraryException {
        List<IArtistDTO> artistDtos = this.artistService.getAll();
        return WSConverter.convertToWSArtists(artistDtos);
    }

    @Override
    public void updateArtist(WSArtist artist) throws MusicLibraryException {
        if (artist == null) {
            throw new MusicLibraryException("Artist can not be null.", "Artist can not be null.");
        }
        IArtistDTO artistDto = WSConverter.convertFromWSArtist(artist);
        try {
            this.artistService.update(artistDto, "rest");
        } catch (Throwable ex) {
            throw new MusicLibraryException(ex.getMessage(), ex.getMessage());
        }
    }

    @Override
    public void deleteArtist(WSArtist artist) throws MusicLibraryException {
        if (artist == null) {
            throw new MusicLibraryException("Artist can not be null.", "Artist can not be null.");
        }
        IArtistDTO artistDto = WSConverter.convertFromWSArtist(artist);
        try {
            this.artistService.delete(artistDto, "rest");
        } catch (Throwable ex) {
            throw new MusicLibraryException(ex.getMessage(), ex.getMessage());
        }
    }

    @Override
    public void createArtist(WSArtist artist) throws MusicLibraryException {
        if (artist == null) {
            throw new MusicLibraryException("Artist can not be null.", "Artist can not be null.");
        }
        IArtistDTO artistDto = WSConverter.convertFromWSArtist(artist);
        try {
            this.artistService.create(artistDto, "rest");
        } catch (Throwable ex) {
            throw new MusicLibraryException(ex.getMessage(), ex.getMessage());
        }
    }
    
    @Override
    public void login(String userName, String password) throws MusicLibraryException {
        try {
            this.authService.login(userName, password);
        } catch (Throwable ex) {
            throw new MusicLibraryException(ex.getMessage(), ex.getMessage());
        }
    }

}